﻿package com.module.device.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.device.dao.DeviceActiveStatusDao;
import com.module.device.vo.DeviceActiveStatus;

public class DeviceActiveStatusManager extends
		GenericHibernateManager<DeviceActiveStatus, Long> {

	private DeviceActiveStatusManager() {
	}

	private static DeviceActiveStatusManager manager;

	public synchronized static DeviceActiveStatusManager getInstance() {
		if (manager == null)
			manager = new DeviceActiveStatusManager();
		return manager;
	}

	private DeviceActiveStatusDao dao = DeviceActiveStatusDao.getInstance();

	/**
	 * 查询列表
	 * 
	 * @param pageObj
	 * @param pu
	 * @return
	 * @throws Exception
	 */
	public Page list(Page pageObj, DeviceActiveStatus pu) throws Exception {
		Session session = this.getSession();
		Page page = new Page();
		try {
			page = dao.getList(session, pu, pageObj.getStart(),
					pageObj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}

	/**
	 * 新增/修改
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> add(DeviceActiveStatus obj) throws Exception {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			if (null != obj.getId() && 0 != obj.getId()) {// 修改

				flag = dao.update(session, obj);

			} else {

				flag = dao.save(session, obj);
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		map = optHandle(flag);
		return map;
	}

	/**
	 * 删除任务
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> delete(Long id) throws Exception {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		Map<String, Object> map = new HashMap<String, Object>();
		boolean flag = false;
		try {
			DeviceActiveStatus obj = dao.get(session, id);

			flag = dao.update(session, obj);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;

		} finally {
			this.closeSession();
		}
		map = optHandle(flag);
		return map;

	}
	   
		  /**
		   * 根据softwareCode查找
		   * @param id
		   * @return
		   * @throws Exception
		   */
		   public List findBysoftwareCode(String softwareCode) throws Exception{
			   Session session = this.getSession();
			   List vo = new ArrayList();
			   try{
				   vo = dao.findeBySoftwareCode(session, softwareCode);
			   }catch (Exception e) {
					throw e;
			   }finally{
				   this.closeSession();
			   }
			   return vo;
		   }
	   

	/**
	 * 根据id查找
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public DeviceActiveStatus findById(Long id) throws Exception {
		Session session = this.getSession();
		DeviceActiveStatus vo = new DeviceActiveStatus();
		try {
			vo = dao.get(session, id);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return vo;
	}

	/**
	 * 根据设备软件编号获取运行结果
	 * 
	 * @return
	 * @throws Exception 
	 */
	public DeviceActiveStatus getRunInfoById(String softwareCode) throws Exception {
		Session session = this.getSession();
		DeviceActiveStatus vo = new DeviceActiveStatus();
		try {
			vo = dao.findUniqueObjectBySQL(session, "from DeviceActiveStatus where softwareCode='"+softwareCode+"'");
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return vo;
	}
	
	/**
	 * 根据事件ID获取设备运行明细
	 * @return
	 * @throws Exception 
	 */
	public List getRunInfoByEventId(int eventId) throws Exception{
		Session session = this.getSession();
		
		try {
			String sql = "select distinct t5.c_device_type_name,t4.c_device_code,t1.c_driverid,t2.c_active_status,t1.d_createtime,t6.c_user_truename,t7.c_event_name " +
					     "from (select e1.* from ts_event_record e1,(" +
					     "select i_sourceid,c_driverid,max(d_createtime) as d_createtime from ts_event_record group by i_sourceid,c_driverid) e2 " +
					     "where e1.i_sourceid=e2.i_sourceid and e1.c_driverid=e2.c_driverid and e1.d_createtime=e2.d_createtime " +
					     ") t1 " +
					     "left join device_active_status t2 on t2.c_software_code=t1.c_driverid " +
					     "left join device_lifecycle t3 on t1.c_driverid=t3.c_software_code and c_usable='Y' " +
					     "left join device_entity t4 on t4.i_id=t3.i_entity_id " +
					     "left join device_type t5 on t4.i_type_id=t5.i_id " +
					     "left join prms_user t6 on t1.c_createrid=t6.c_user_id " +
					     "left join ts_ent_event t7 on t1.i_sourceid=t7.i_id " +
					     "where t1.i_sourceid="+eventId+" and t4.c_device_code is not null";
			
			Query query = (Query) session.createSQLQuery(sql);
			List list =  query.list();
			return list;
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		
	}
	

	/**
	 * 操作处理返回
	 * 
	 * @param isTrue
	 * @return
	 */
	public static Map<String, Object> optHandle(boolean isTrue) {
		String status = null;
		status = (isTrue == true ? "操作成功！" : "操作失败！");
		return optHandle(isTrue, status);
	}

	/**
	 * 操作处理返回
	 * 
	 * @param isTrue
	 * @return
	 */
	public static Map<String, Object> optHandle(boolean isTrue, String message) {
		Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("state", isTrue);
		statusMap.put("msg", message);
		return statusMap;
	}

	@Override
	public GenericHibernateDao<DeviceActiveStatus, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
}
